﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFExcelReport.Helpers;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class TCPGB
    {
        
        #region ==dwGAB===========
        public static readonly DependencyProperty dwGABProperty = DependencyProperty.Register("dwGAB",
            typeof(DataRow), typeof(TCPGB),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwGABChanged));
        public DataRow dwGAB
        {
            get
            {
                return (DataRow)GetValue(dwGABProperty);
            }
            set
            {
                SetValue(dwGABProperty, value);
            }
        }
        private static void OndwGABChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TCPGB;
            if (src != null)
            {
                src.dwGAB = (DataRow)e.NewValue;
            }
        }
        #endregion 

        public TCPGB()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_GLLG();
            this.DataContext = MV; 
            InitializeComponent(); 
        }
       
        public void LOADBYGB(string partie)
        {
            try
            {
                TBMain.Clear();
                if (partie.Length < 7)
                    return;
                dwData = TBMain.NewRow();
                dwData["GLMANDANT"] = app.Mandant;
                dwData["GLLO"] = LO;
                dwData["GLLB"] = LB;
                dwData["ICCDAT"] = FROMDATE;
                dwData["GLPRODDAT"] = ENDDATE;
                dwData["GLPARTIE"] = partie;
                SResult = ((VICC_GLLG)MV).QueryICC_GLLG(dwData, 1);
                MergeData(SResult, true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
       
        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {                
                txtDAT.Text = Helpers.Helper.GetDate(sender);
                if (dwMaster == null) return;
                dwMaster["ICCDAT"]=txtDAT.Text.ToString().Trim();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
       
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                ((VICC_GLLG)MV).BATCHGBCHG();
                base.SaveCommand(sender, e);
                IsEdit = false;
                LOADBYGB(PARTIE);
                DT.Text = "";
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            if (IsEdit)
            {
                MessageBox.Show("已处于编辑状态，请先将数据保存后才可再次进行编辑！");
                return;
            }            
            MV.TBTemp = null;
            MV.TBTemp = TBMain.Copy();
            base.EditCommand(sender, e);
            IsEdit = true;
        }
        private void DT2ALLCommand(object sender, RoutedEventArgs e)
        {
            if (!IsEdit)
                return;
            //if (dwMaster == null)
            //    return;
            //foreach (DataRow row in TBMain.Rows)
            //{
            //    row["ICCDAT"] = dwMaster["ICCDAT"];
            //}   
            string dd = txtDAT.Text.ToString().Trim();
            if (TBMain.Rows.Count == 0)
                return;
            DataTable tb = TBMain.Clone();
            foreach (var item in dlgMaster.SelectedItems)
            {
                if (!item.GetType().ToString().Contains("DataRowView")) continue;

                DataRow dwItem = ((System.Data.DataRowView)item).Row;
                DataRow _dwWB = tb.Rows.Add();
                _dwWB.ItemArray = dwItem.ItemArray;
            }            
            foreach (DataRow row in tb.Rows)
            {
                filter = "ICCDAT='" + row["ICCDAT"].ToString() + "' AND  GLPARTIE='" + row["GLPARTIE"].ToString() + "'  AND  GLLBEHI='" + row["GLLBEHI"].ToString() + "' ";
                rows = TBMain.Select(filter);
                foreach (DataRow dwWB in rows)
                {
                    dwWB["ICCDAT"] = dd;
                }
            }

        }
        public override void OnPARTIEChg(string value)
        {
            LOADBYGB(value);
            IsEdit = false;
            base.OnPARTIEChg(value);
        }

    }
}
